{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 使用特殊库函数 random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.34349485 0.82660757 0.16610837]\n",
      " [0.88321238 0.43419877 0.92991106]\n",
      " [0.16802067 0.0066936  0.16106313]\n",
      " [0.27821483 0.92965145 0.42839245]]\n",
      "[[0.270794   0.00744393 0.68336381]\n",
      " [0.51588723 0.13328178 0.2942632 ]\n",
      " [0.83171662 0.63395131 0.66927295]\n",
      " [0.54370458 0.81048736 0.4602257 ]]\n",
      "[[25  6 41]\n",
      " [73 42 71]\n",
      " [19  5  9]\n",
      " [22 47 23]]\n",
      "[[-1.79866421  0.43494613  0.44966472]\n",
      " [-0.47830591 -0.99684609 -0.81483241]\n",
      " [ 0.56904102  2.17373727 -0.13293669]\n",
      " [ 0.02649967  0.93229658 -0.62018928]]\n",
      "[[-1.25354281 -0.11528983  0.88912333]\n",
      " [-0.37620873 -0.4963824   0.15183894]\n",
      " [ 0.36081115  0.44928017 -1.26380996]\n",
      " [ 0.34466195 -2.09778082  0.62814394]]\n",
      "[[-0.6117637  -0.16798357 -0.19058515]\n",
      " [ 1.40941828  0.71195996 -1.33969043]\n",
      " [-0.81740113  1.71599274  1.01623357]\n",
      " [-0.41235297 -1.34373513 -0.87104424]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.rand(4, 3) # 随机小数\n",
    "print(arr1)\n",
    "arr2 = np.random.random(size=(4, 3))    # 随机小数\n",
    "print(arr2)\n",
    "arr3 = np.random.randint(0, 100, size=(4, 3))   # 随机整数\n",
    "print(arr3)\n",
    "arr4 = np.random.randn(4, 3)    # 均匀分布\n",
    "print(arr4)\n",
    "arr5 = np.random.normal(size=(4, 3))    # 正态分布\n",
    "print(arr5)\n",
    "arr6 = np.random.standard_normal(size=(4, 3))   # 标准正态分布\n",
    "print(arr6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将Python array_like对象转换为Numpy数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n",
      "[10. 20. 30.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 初始化列表和元组\n",
    "a = [1,2,3]\n",
    "b = (10,20,30)\n",
    "\n",
    "print(np.array(a, dtype=np.float64))\n",
    "print(np.array(b, dtype=np.float64))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内在的numpy数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.   1.9  2.8  3.7  4.6  5.5  6.4  7.3  8.2  9.1 10. ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "array = np.linspace(1, 10, 11, endpoint=True)\n",
    "print(array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将中文字符串编码为utf-8字节码，读取字节流为ndarry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'\\xe4\\xbd\\xa0\\xe5' b'\\xa5\\xbd\\xef\\xbc' b'\\x8c\\xe6\\x95\\xb0'\n",
      " b'\\xe6\\x8d\\xae\\xe5' b'\\x88\\x86\\xe6\\x9e' b'\\x90\\xef\\xbc\\x81']\n"
     ]
    }
   ],
   "source": [
    "s =  '你好，数据分析！' \n",
    "s_encoded = s.encode('utf-8')\n",
    "a = np.frombuffer(s_encoded, dtype =  'S1')  \n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成3行5列整型随机数，范围介于66到88之间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[83 70 84 72 73]\n",
      " [69 73 76 82 79]\n",
      " [80 73 73 68 85]]\n"
     ]
    }
   ],
   "source": [
    "num = np.random.randint(66, 88, size=(3, 5))\n",
    "print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "随机生成3次生产的月饼尺寸(cm),一组20个月饼。\n",
    "数量:3*20=60\n",
    "随机生成的月饼尺寸，服从正态分布,均值6，方差0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6.42038667 5.85414694 5.98568942 6.00489779 6.00272073 5.97381602\n",
      "  6.13406279 6.20026146 5.77791803 6.13515921 5.84457411 5.99854416\n",
      "  5.92460952 6.13769417 6.54190389 6.17812575 5.92129061 5.83192295\n",
      "  5.63983703 6.03591422]\n",
      " [5.7537767  5.85180636 5.88700212 5.62811333 6.04740359 5.75838799\n",
      "  6.08628479 6.16588311 5.55076053 6.02664187 5.83215886 6.23747538\n",
      "  5.96857816 5.52338371 6.22531719 6.3167788  6.02257248 6.36365445\n",
      "  6.18115192 5.67536351]\n",
      " [5.93324165 6.07873823 5.94750741 6.40878565 5.68453783 5.83670894\n",
      "  5.81795839 6.17800238 5.79167343 5.95043412 5.9963976  5.62514896\n",
      "  6.21796164 6.48951665 5.78530491 5.9267277  6.53299553 5.95033412\n",
      "  5.88225777 6.23275941]]\n"
     ]
    }
   ],
   "source": [
    "moon_cake = np.random.normal(6,0.2,size=(3,20))\n",
    "print(moon_cake)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
